/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebServiceGamingServer;

import WebServiceDBController.GamingStateDB;
import WebServiceModel.GamingStateModel;
import java.util.ArrayList;

/**
 *
 * @author Peach
 */
public class GamingServer {
    public ArrayList<String> getMessage(String deskId){
	//return current desk state;
        GamingStateDB db=new GamingStateDB();
        return changeToArray(db.GetState(deskId));
    }
	
    public void sendMessage(ArrayList<String> state){
        GamingStateDB db=new GamingStateDB();
        db.ModifyOption(changeToModel(state));
    }
    
    public void gameOver(String deskId){
        GamingStateDB db=new GamingStateDB();
        GamingStateModel state=db.GetState(deskId);
        db.ClearOption(state);
    }
    
    private ArrayList<String> changeToArray(GamingStateModel state){
        ArrayList<String> stateArray=new ArrayList<String>();
        stateArray.add(state.getDeskID());
        stateArray.add(state.getState());
        stateArray.add(state.getPos().x+"");
        stateArray.add(state.getPos().y+"");
        stateArray.add(state.getTime()+"");
        return stateArray;
    }
    
    private GamingStateModel changeToModel(ArrayList<String> state){
        GamingStateModel stateModel= new GamingStateModel();
        stateModel.setDeskID(state.get(0));
        stateModel.setState(state.get(1));
        stateModel.setPos(Integer.parseInt(state.get(2).trim()),
                Integer.parseInt(state.get(3).trim()));
        stateModel.setTime(Integer.parseInt(state.get(4).trim()));
        return stateModel;
    }
}
